package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.DishDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 新增菜品
     * @param dishDTO
     */
    @Override
    @Transactional
    @CacheEvict(cacheNames = "cate_dish",allEntries = true)
    public void insertDish(DishDTO dishDTO) {
        //创建一个类,存放响应数据,并设置数据
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dish.setCreateTime(LocalDateTime.now());
        dish.setUpdateTime(LocalDateTime.now());
        dish.setCreateUser(BaseContext.getCurrentId());
        dish.setUpdateUser(BaseContext.getCurrentId());
        //插入数据,返回id字段内容
        dishMapper.insertDish(dish);
        Long dishId = dish.getId();
        log.info("id是{}",dishId);
        //获取口味
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && flavors.size() > 0)
        dishFlavorMapper.insertbatch(flavors,dishId);

    }

    /**
     * 菜品分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult DishQueryingAll(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        List<Dish> dishVO = dishMapper.DishQueryingAll(setmealPageQueryDTO);
        Page<Dish> dishVO1 = (Page<Dish>) dishVO;
        long total = dishVO1.getTotal();
        List<Dish> result = dishVO1.getResult();
        return new PageResult(total,result);
    }

    /**
     * 根据id查菜品
     * @param id
     * @return
     */
    @Override
    public DishVO IdQueryingDish(String id) {
        //查询菜品表
        DishVO dishVO = dishMapper.IdQueryingDish(id);
        //查询口味表
        List<DishFlavor> dishFlavors = dishFlavorMapper.tasteQuery(id);
        Long categoryId = dishVO.getCategoryId();
        //查询套餐名称
        String name = categoryMapper.IdQueryingcategor(categoryId);
        dishVO.setFlavors(dishFlavors);
        dishVO.setCategoryName(name);
        return dishVO;
    }

    /**
     * 修改菜品
     * @param dishDTO
     */
    @Override
    @Transactional
    @CacheEvict(cacheNames = "cate_dish",allEntries = true)
    public void modifyTheDishes(DishDTO dishDTO) {
        //创建菜品表,添加最后更新时间
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dish.setUpdateTime(LocalDateTime.now());
        dish.setUpdateUser(BaseContext.getCurrentId());
        //添加菜品
        dishMapper.modifyTheDishes(dish);

        List<DishFlavor> flavors = dishDTO.getFlavors();
        Long dish_id = dishDTO.getId();
        dishFlavorMapper.deleteFlavor(dish_id);
        //添加删除之后修改的口味
        if (flavors!=null && flavors.size()>0)
            dishFlavorMapper.InsertionFlavor(dish_id,flavors);
    }
    /**
     * 菜品起售,停售
     * @param status
     * @param id
     */
    @Override
    @CacheEvict(cacheNames = "cate_dish",allEntries = true)
    public void dishStatus(String status, String id) {
        dishMapper.dishStatus(status,id);
    }

    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
     */
    @Override
    public List<Dish> IdClassification(String categoryId) {
        List<Dish> dishes = dishMapper.IdClassification(categoryId);
        return dishes;
    }

    /**
     * 批量删除菜品
     * @param ids
     */
    //同上
    @Override
    @Transactional
    @CacheEvict(cacheNames = "cate_dish",allEntries = true)
    public String deleteInBatches2(Integer[] ids) {

        for (Integer dishId : ids) {
            //判断状态是否是停售
            int i = dishMapper.A1(dishId, StatusConstant.ENABLE);
            int i1 = dishMapper.A2(dishId);
            if (i > 0){
                return MessageConstant.DISH_ON_SALE;
            }
            if (i1 > 0){
                return MessageConstant.DISH_BE_RELATED_BY_SETMEAL;
            }
        }
        for (Integer dishId : ids) {
            dishMapper.deleteInBatches(dishId.toString());
            dishFlavorMapper.removeFlavors(dishId.toString());
        }
        return null;
    }

    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
     */
    @Override
    @Cacheable(value = "cate_dish",key = "'list'+#categoryId")
    public List<DishVO> SortFindService(String categoryId) {
        Integer shopOpen = StatusConstant.SHOP_OPEN;
        List<DishVO> dishVOS = dishMapper.SortFindService(categoryId, shopOpen);
        return dishVOS;
    }
}
